package com.gxuwz.attendance.action;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import com.gxuwz.attendance.entity.Teacher;
import com.gxuwz.attendance.service.CollegeService;
import com.gxuwz.attendance.service.TeacherService;
import com.gxuwz.attendance.util.JsonUtil;
import com.gxuwz.attendance.vo.Page;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class TeacherAction extends BaseAction implements ModelDriven<Teacher>, Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static String TEACHER_INDEX = "/page/teacher/teacher_list.jsp";
	@Autowired
	private TeacherService service;
	@Autowired
	private CollegeService collegeService;
	private Teacher teacher;
	private String rows;
	private String page;
	private String type;
	private String num = null;
	private String data;
	private String classNo;
	private String ids;
	private Map<String, Object> map = new HashMap<String, Object>();
	
	/**
	 * 返回教师页面
	 * @return
	 */
	public String index() {
		collegeService.findByName(null);
		forwardView = TEACHER_INDEX;
		return SUCCESS; 
	}
	
	/**
	 * 模糊查询教师信息
	 */
	public void list() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		
		map.put("rows", service.findByPage(teacher, new Page(Integer.parseInt(page), Integer.parseInt(rows))));
		map.put("total", session.getAttribute("teacherSize"));
		
		JsonUtil.toJons(ServletActionContext.getResponse(), map);
		
	}
	
	/**
	 * 添加教师信息
	 */
	public void add() {
		boolean flag = service.addTeacher(teacher);
		
		if(flag != true) {
			map.put("type", "error");
			map.put("msg", "添加失败");
		}else {
			map.put("type", "success");
			map.put("msg", "添加成功");
		}
		
		JsonUtil.toJons(ServletActionContext.getResponse(), map);
	}
	
	/**
	 * 删除教师信息
	 */
	public void delete() {
		boolean flag = true;
		try {
			flag = service.deleteTeacher(ids);
		}catch (Exception e) {
			map.put("type", "error");
			map.put("msg", "删除失败");
			e.printStackTrace();
		}
		map.put("type", "success");
		map.put("msg", "删除成功");
		if(flag == false) {
			map.put("type", "error");
			map.put("msg", "删除失败");
		}
		JsonUtil.toJons(ServletActionContext.getResponse(), map);
	}
	
	/**
	 * 修改教师信息
	 */
	public void update() {
		boolean flag = service.updateTeacher(teacher);
		if(flag == false) {
			map.put("type", "error");
			map.put("msg", "修改失败");
		}else {
			map.put("type", "success");
			map.put("msg", "修改成功");
		}
		
		JsonUtil.toJons(ServletActionContext.getResponse(), map);
	}
	


	@Override
	public void prepare() throws Exception {
		if (teacher == null) {
			teacher = new Teacher();
		}
	}

	@Override
	public Teacher getModel() {
		return teacher;
	}

	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getNum() {
		return num;
	}

	public void setNum(String num) {
		this.num = num;
	}


	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

	public String getClassNo() {
		return classNo;
	}

	public void setClassNo(String classNo) {
		this.classNo = classNo;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

}
